//
//  YSLoginViewController.m
//  YouSheng
//
//  Created by aaa on 16/8/18.
//  Copyright © 2016年 FanShuHan. All rights reserved.
//

#import "YSLoginViewController.h"
#import <SVProgressHUD.h>
#import "NSString+YS.h"
#import "YSLoginManager.h"
#import "YSService.h"
#import "YSMainTabBarController.h"
#import "UIView+Toast.h"

@interface YSLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation YSLoginViewController


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self configNav];
    [self configStatusBar];

}
- (void)configNav{
    self.navigationItem.title = @"";

    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowOffset = CGSizeMake(0, 0);
    self.navigationController.navigationBar.titleTextAttributes =@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                                                                   NSForegroundColorAttributeName:[UIColor whiteColor]};
}

- (void)configStatusBar{
    UIStatusBarStyle style = [self preferredStatusBarStyle];
    [[UIApplication sharedApplication] setStatusBarStyle:style
                                                animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)login:(id)sender {
    [self doLogin];
}

- (void)doLogin {
    [_usernameTextField resignFirstResponder];
    [_passwordTextField resignFirstResponder];
    
    NSString *username = [_usernameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password = _passwordTextField.text;
    [SVProgressHUD show];
    
    NSString *loginAccount = username;
    NSString *loginToken   = [password tokenByPassword];
    
    //NIM SDK 只提供消息通道，并不依赖用户业务逻辑，开发者需要为每个APP用户指定一个NIM帐号，NIM只负责验证NIM的帐号即可(在服务器端集成)
    //用户APP的帐号体系和 NIM SDK 并没有直接关系
    //DEMO中使用 username 作为 NIM 的account ，md5(password) 作为 token
    //开发者需要根据自己的实际情况配置自身用户系统和 NIM 用户系统的关系
    [[[NIMSDK sharedSDK] loginManager] login:loginAccount
                                       token:loginToken
                                  completion:^(NSError * _Nullable error) {
                                      [SVProgressHUD dismiss];
                                      if (error == nil) {
                                          LoginData *sdkData = [[LoginData alloc] init];
                                          sdkData.account = loginAccount;
                                          sdkData.token = loginToken;
                                          [[YSLoginManager sharedManager] setCurrentLoginData:sdkData];
                                          [[YSServiceManager sharedManager] start];
                                          
                                          YSMainTabBarController *main = [[YSMainTabBarController alloc] initWithNibName:nil bundle:nil];
                                          [UIApplication sharedApplication].keyWindow.rootViewController = main;
                                      } else {
                                          NSString *toast = [NSString stringWithFormat:@"登录失败 code: %zd",error.code];
                                          [self.view makeToast:toast duration:2.0 position:CSToastPositionCenter];
                                      }
    }];
}

@end
